<?php

/**
 * pages actions.
 *
 * @package    shop
 * @subpackage pages
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pagesActions extends sfActionsEx
{
    public function executeIndex(){
      $this->newses = NewsTable::getInstance()->createQuery()->select()->orderBy('create_time DESC')->limit(5)->execute();
      $this->news = $this->newses->getFirst();
      if($this->news){
        unset($this->newses[0]);
      }
      $this->carouselElements = CarouselTable::getInstance()->findAll();
    } 
       
    public function executeAccountAdminister(sfRequest $request){
        if(!Authorization::getInstance()->isExists()){
            $this->redirect('@index');
        }
        $this->user = Authorization::getInstance()->getUser();
        if($this->user){
            $this->deliveryData = DeliveryDataTable::getInstance()->findOneBy('user_id', $this->user->getId());
            if($this->user->getEmail() != ""){
                $this->newsletter = NewsletterTable::getInstance()->findOneBy('email', $this->user->getEmail());
            }
            if(!$this->deliveryData){
                $this->redirect('pages/index');
            }
            $this->orders = $this->deliveryData->getOrders();
        }
        else{
            $this->redirect('pages/index');
        }
    }
    
    public function executeRegisterAccount(sfRequest $request) {
        if(Authorization::getInstance()->isExists()){
            $this->redirect('@accountAdminister');
        }
    }
    
    public function executeLogin(sfRequest $request){
        if(Authorization::getInstance()->isExists()){
            $this->redirect('@accountAdminister');
        }
    }
    
    public function executeStaticPage(sfRequest $request) {
        if($request->getParameter('url') != NULL){
            $page = PagesTable::getInstance()->findOneBy('route', $request->getParameter('url'));
            if($page){
                $this->page = $page;
            }
            else{
                $this->redirect('pages/index');
            }
        }
        else{
            $this->redirect('pages/index');
        }
    }
    
    public function executeOffer(sfRequest $request){
        
    }
    
    public function executeBasket(sfRequest $request) {
        $this->user = Authorization::getInstance()->getUser();
        $this->deliveryData = NULL;
        if($this->user){
            $this->deliveryData = $this->user->getDeliveryData()->getFirst();
        }
        $this->basket = myBasket::getInstance();
    }
    
    public function executeContact(sfRequest $request) {

    }
    
    public function executeNews(sfRequest $request){
        if($request->getParameter('id') != NULL){
            $news = NewsTable::getInstance()->findOneBy('id', $request->getParameter('id'));
            if($news){
                $this->news = $news;
            }
            else{
                $this->redirect('pages/index');
            }
        }
        else{
            $this->redirect('pages/index');
        }
    }
}   
